package com.css.fxfzypg.modules.resultManagement.reportManagement.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.resultManagement.reportManagement.VO.FieldReportManagementVO;
import com.css.fxfzypg.modules.resultManagement.reportManagement.VO.ReportInfoVO;
import com.css.fxfzypg.modules.resultManagement.reportManagement.VO.ReportManagementVO;
import com.css.fxfzypg.modules.resultManagement.reportManagement.VO.SamplingQueryVO;
import com.css.fxfzypg.modules.resultManagement.reportManagement.entity.FieldReportManagement;
import com.css.fxfzypg.modules.resultManagement.reportManagement.service.SamplingSurveyReportService2;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2022年12月08日  13:51
 */
@RestController
@Validated
@RequestMapping("/samplingReport")
public class SamplingSurveyReportManageMentController {

    @Resource
    private SamplingSurveyReportService2 samplingSurveyReportService;
    /***
     * （重新）生成报告
     * @param vo
     * @return
     */
    @PostMapping("/createReport")
    public RestResponse createReport(@RequestBody SamplingQueryVO vo, HttpServletResponse response ) throws Exception {
        return samplingSurveyReportService.createReport(vo.getTaskCode(),vo.getTaskName(),vo.getModelName(),vo.getAttachId(),response);
    }

    /***
     * 查询分页
     * @param vo
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(ReportManagementVO vo, HttpServletRequest request){
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=samplingSurveyReportService.getPage(vo,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /***
     * 上传报告
     * @param file
     * @return
     */
    @PostMapping("/uploadReport")
    public RestResponse uploadReport(@RequestParam("file") MultipartFile file, @RequestParam("taskId") String taskId){
        return samplingSurveyReportService.uploadReport(file,taskId);
    }

    /***
     * 查询任务下的报告信息
     * @param taskId
     * @return
     */
    @GetMapping("/getReportByTaskId")
    public RestResponse getReportByTaskId(@RequestParam("id") String taskId){
        FieldReportManagement reportByTaskId =samplingSurveyReportService.getReportByTaskId(taskId);
        return ResponseCreator.createSuccessResponse(reportByTaskId);
    }

    /***
     * 报告下载
     * @param reportId
     * @param def 1系统默认 2、用户自己的
     * @return
     */
    @PostMapping("/downloadReport")
    public void downloadReport(@RequestParam("reportId")String reportId,@RequestParam("def")String def,HttpServletResponse res){
        samplingSurveyReportService.downloadReport(reportId,def,res);
    }


    @GetMapping("/getHistoryReportByTaskId")
    public RestResponse getHistoryReportByTaskId(@RequestParam("id") String taskId, HttpServletRequest request){
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=samplingSurveyReportService.getHistoryReportByTaskId(taskId,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    @GetMapping("/getReportListByTaskId")
    public RestResponse getReportListByTaskId(@RequestParam("id") String taskId, HttpServletRequest request){
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=samplingSurveyReportService.getReportListByTaskId(taskId,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /***
     * 历史报告下载
     * @param reportId

     * @return
     */
    @PostMapping("/downloadHistoryReport")
    public void downloadHistoryReport(@RequestParam("reportId")String reportId,HttpServletResponse res){
        samplingSurveyReportService.downloadHistoryReport(reportId,res);
    }

    /**
     *获取审核通过后的报告信息
     * @return
     */
    @GetMapping("/getFieldReport")
    public List<FieldReportManagementVO> getFieldReport(){
   /*     RestResponse response = null;
        try{
            List<ReportInfoVO> list=reportManagementService.getReportInfo();
            response = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            //log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;*/
        List<FieldReportManagementVO> list=samplingSurveyReportService.getFieldReport();
        return list;
    }
}
